package net.maku.events.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import net.maku.events.vo.ActivityTopVO;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.common.utils.Result;
import net.maku.events.convert.ActivitySnapshotConvert;
import net.maku.events.entity.ActivitySnapshotEntity;
import net.maku.events.service.ActivitySnapshotService;
import net.maku.events.query.ActivitySnapshotQuery;
import net.maku.events.vo.ActivitySnapshotVO;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;

import java.util.List;

/**
 * 随手拍活动
 *
 * @author 阿沐 babamu@126.com
 * @since 1.0.0 2025-03-22
 */
@RestController
@RequestMapping("events/activity_snapshot")
@Tag(name = "随手拍活动")
@AllArgsConstructor
public class ActivitySnapshotController {
    private final ActivitySnapshotService activitySnapshotService;

    @GetMapping("page")
    @Operation(summary = "分页")
    @PreAuthorize("hasAuthority('events:activity_snapshot:page')")
    public Result<PageResult<ActivitySnapshotVO>> page(@ParameterObject @Valid ActivitySnapshotQuery query) {
        PageResult<ActivitySnapshotVO> page = activitySnapshotService.page(query);

        return Result.ok(page);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @PreAuthorize("hasAuthority('events:activity_snapshot:info')")
    public Result<ActivitySnapshotVO> get(@PathVariable("id") Long id) {
        ActivitySnapshotEntity entity = activitySnapshotService.getById(id);

        return Result.ok(ActivitySnapshotConvert.INSTANCE.convert(entity));
    }

    @PostMapping
    @Operation(summary = "保存")
    @PreAuthorize("hasAuthority('events:activity_snapshot:save')")
    public Result<String> save(@RequestBody ActivitySnapshotVO vo) {
        activitySnapshotService.save(vo);

        return Result.ok();
    }

    @PutMapping
    @Operation(summary = "修改")
    @PreAuthorize("hasAuthority('events:activity_snapshot:update')")
    public Result<String> update(@RequestBody @Valid ActivitySnapshotVO vo) {
        activitySnapshotService.update(vo);

        return Result.ok();
    }


    @GetMapping("/status/{id}/{status}")
    @Operation(summary = "上下架")
    @PreAuthorize("hasAuthority('events:activity_snapshot:status')")
    public Result<String> updateStatus(@PathVariable("id") Long id, @PathVariable("status") Integer status) {
        activitySnapshotService.updateStatus(id, status);
        return Result.ok();
    }

    @PostMapping("/whether/top")
    @Operation(summary = "是否置顶")
    @PreAuthorize("hasAuthority('events:activity_snapshot:top')")
    public Result<String> whetherTop(@RequestBody ActivityTopVO activityTopVO) {
        activitySnapshotService.whetherTop(activityTopVO);
        return Result.ok();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @PreAuthorize("hasAuthority('events:activity_snapshot:delete')")
    public Result<String> delete(@RequestBody List<Long> idList) {
        activitySnapshotService.delete(idList);

        return Result.ok();
    }
}